import sys
from PyQt4 import QtCore, QtGui
from lib.papyDB import papyDB
from lib import papyDBTable
papyDB = papyDB()




class treeItem:
    def retData(self):
        returnData = [('captin ohho', [('Support', [("sh403", []), ("sh864", []), ("sh801", []), ("sh839", []), ("sh918", []), ("sh009", [])]), ('Sudden', []), ('Garden', []), ('Operation', []), ('Stitch', []), ('Chin', []), ('Side', [])]), ('BANANA', [('Till', []), ('Button', []), ('Will', []), ('Steam', []), ('Produce', []), ('Shut', []), ('Knife', []), ('Letter', []), ('Reward', []), ('Cotton', []), ('Table', []), ('Spoon', []), ('Silver', [])]), ('testPrj', [('Flame', []), ('Hospital', []), ('Train', []), ('Why', []), ('Hear', []), ('Test', []), ('Amount', []), ('Then', []), ('Strong', [])]), ('Disney', [('Minute', []), ('Butter', []), ('Kettle', []), ('Substance', []), ('Very', []), ('Wool', []), ('Tight', []), ('Reward', []), ('Acid', []), ('Surprise', [])]), ('Pooh', [('Over', []), ('Minute', []), ('Flat', []), ('Please', []), ('Wrong', [])]), ('MISSION IMPOSSIBLE', [('Loose', []), ('Test', [])])]
        print returnData
        return returnData
    
    
    
    
class treeModel( QtCore.QAbstractTableModel ):
    def __init__(self, parent=None):
        super(treeModel, self).__init__(parent)
#        self.data = []

    
    def addItems(self, parent, elements):
        for text, children in elements:
            item = QtCore.QAbstractItemModel(text)
            parent.appendRow(item)
            if children:
                self.addItems(item, children)


    def view(self): 
        self.treeView = QtGui.QTreeView()
        self.treeView.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
#        self.data = []
        self.data = treeItem.retData()
        self.addItems(self.model, self.data)
        self.treeView.setModel(self.model)
        self.model.setHorizontalHeaderLabels([self.tr("PAPYRUS")])
        layout = QtGui.QVBoxLayout()
        layout.addWidget(self.treeView)
        self.setLayout(layout)
        



    









class Window(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)        
        self.treeView = QtGui.QTreeView()
        self.treeView.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
        self.treeView.customContextMenuRequested.connect(self.openMenu)

        data = self.data()
        self.model = QtGui.QStandardItemModel()
#        self.model = QtCore.QAbstractItemModel()
        self.addItems(self.model, data)
        self.treeView.setModel(self.model)
        self.model.setHorizontalHeaderLabels([self.tr("PAPYRUS")])
        layout = QtGui.QVBoxLayout()
        layout.addWidget(self.treeView)
        self.setLayout(layout)
    
    def data(self):
        temp = ''
        data = [ ]
        dataList = [  ]
        prjList = papyDBTable.PROJECT.search('prjName')
        for x in range(len(prjList)):
            seqList = papyDBTable.SEQUENCES.search( 'seqName' , prjID = x+1 )
            for y in range(len(seqList)):
                temp +=', ("' + str(seqList[y]) + '", [])'    
            data.append( temp[2:] )  
            temp = ''        
        for x in range(len(data)):
            temp = '( "' + prjList[x] + '", [' + data[x] + '] )'
            dataList.append(str(temp))
        temp = ''
        for x in range(len(dataList)):
            temp += dataList[x] + ',' 
        returnData = list(eval(temp[:-1]))
        print returnData
        returnData = [('captin ohho', [('Support', [("sh403", []), ("sh864", []), ("sh801", []), ("sh839", []), ("sh918", []), ("sh009", [])]), ('Sudden', []), ('Garden', []), ('Operation', []), ('Stitch', []), ('Chin', []), ('Side', [])]), ('BANANA', [('Till', []), ('Button', []), ('Will', []), ('Steam', []), ('Produce', []), ('Shut', []), ('Knife', []), ('Letter', []), ('Reward', []), ('Cotton', []), ('Table', []), ('Spoon', []), ('Silver', [])]), ('testPrj', [('Flame', []), ('Hospital', []), ('Train', []), ('Why', []), ('Hear', []), ('Test', []), ('Amount', []), ('Then', []), ('Strong', [])]), ('Disney', [('Minute', []), ('Butter', []), ('Kettle', []), ('Substance', []), ('Very', []), ('Wool', []), ('Tight', []), ('Reward', []), ('Acid', []), ('Surprise', [])]), ('Pooh', [('Over', []), ('Minute', []), ('Flat', []), ('Please', []), ('Wrong', [])]), ('MISSION IMPOSSIBLE', [('Loose', []), ('Test', [])])]
        return returnData   


    def data2(self):
        seqNo = []
        prjList = papyDBTable.PROJECT.search('prjName')
        for x in range(len(prjList)):
            seqList = papyDBTable.SEQUENCES.search( 'seqName' , prjID = x+1 )
            seqNo.append(papyDBTable.SEQUENCES.search( 'seqID' , prjID = x+1 ))
            for b in range(len(seqNo[x])):
                print seqNo[x][b] 
            print seqList
            print str(papyDBTable.SHOT.search( 'shotName' , seqID = seqNo[x][b]))
#            print shotList    
                
            
            
            
            
            
            
            
#            for y in range(len(seqList)):
#                temp +=', ("' + str(seqList[y]) + '", [])'    
#            data.append( temp[2:] )  
#            temp = ''        
#        for x in range(len(data)):
#            temp = '( "' + prjList[x] + '", [' + data[x] + '] )'
#            dataList.append(str(temp))
#        temp = ''
#        for x in range(len(dataList)):
#            temp += dataList[x] + ',' 
#        returnData = list(eval(temp[:-1]))
#        print returnData
 
        
        
    
    def addItems(self, parent, elements):
        for text, children in elements:
            item = QtGui.QStandardItem(text)
            parent.appendRow(item)
            if children:
                self.addItems(item, children)

    def openMenu(self, position):
        indexes = self.treeView.selectedIndexes()
        if len(indexes) > 0:
            level = 0
            index = indexes[0]
            while index.parent().isValid():
                index = index.parent()
                level += 1                
        menu = QtGui.QMenu()
        if level == 0:
            menu.addAction(self.tr("Make Project"))
            menu.addAction(self.tr("Add Sequence"))
        elif level == 1:
            menu.addAction(self.tr("Make Sequence"))
            menu.addAction(self.tr("Add Shot"))
        elif level == 2:
            menu.addAction(self.tr("Make Shot"))         
        menu.exec_(self.treeView.viewport().mapToGlobal(position))
      
        
if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    treeModel = treeModel()
    treeModel.view()
#    window = Window()
#    window.show()
#    window.data2()
    sys.exit(app.exec_())







        
